﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace demo09
{
    class Program9
    {
        static void Main(string[] args)
        {
            bool flag = true;
            int rows = 0;

            while (flag)
            {
                try
                {
                    Console.WriteLine("请输入杨辉三角列数：");
                    rows = Convert.ToInt32(Console.ReadLine());
                    flag = false;
                }
                catch
                {
                    Console.WriteLine("输入的不是整数，请重新输入");
                }
            }
            int[, ] arr = new int[rows, 2 * rows - 1];
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    if (j == rows - 1 - i || j == rows - 1 + i)
                    {
                        arr[i, j] = 1;
                    }
                }
            }

            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    if (j == rows - 1 - i && i > 1)
                    {
                        for (int k = 0; k < i - 1; k++)
                        {
                            arr[i, j + 2 * (k + 1)] = arr[i - 1, (j + 2 * (k + 1)) - 1] + arr[i - 1, (j + 2 * (k + 1)) + 1];
                        }
                    }
                }
            }

            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    if (arr[i, j] == 0)
                    {
                        Console.Write("  ");
                    }
                    else
                    {
                        Console.Write(" " + arr[i,j]);
                    }
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}
